<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="showRoleDialog"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="roleList">
          <el-table-column
            align="center"
            type="index"
            label="序号"
            width="120"
          />
          <el-table-column
            align="center"
            prop="name"
            label="名称"
            width="240"
          />
          <el-table-column align="center" prop="description" label="描述" />
          <el-table-column label="操作" width="240" align="center">
            <template #default="{row}">
              <el-button size="small">分配权限</el-button>
              <el-button size="small" @click="editRole(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination
            layout="prev,pager,next"
            :current-page="params.page"
            :total="total"
            :page-size="params.pagesize"
            @current-change="pageChange"
          />
        </el-row>
      </el-card>

      <!-- 新增角色 -->
      <add-role ref="addRole" :add-visible="addRoleVisible" @close-dialog="closeRoleDialog" @fetch-role-list="fetchRoleList" />

    </div>
  </div>
</template>
<script>
import { getRoleList, deleteRole } from '@/api/setting'
import AddRole from '@/views/Setting/component/add-role.vue'
export default {
  components: {
    AddRole
  },
  data() {
    return {
      roleList: [],
      // 分页器数据
      params: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      addRoleVisible: false
    }
  },
  mounted() {
    this.fetchRoleList()
  },
  methods: {
    async fetchRoleList() {
      const res = await getRoleList(this.params)
      this.roleList = res.rows
      this.total = res.total
    },
    pageChange(newPage) {
      this.params.page = newPage
      this.fetchRoleList()
    },
    delRole(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 调用删除接口
        await deleteRole(id)
        // 更新列表
        this.fetchRoleList()
        // 提示用户
        this.$message.success('删除角色成功')
      }).catch()
    },
    showRoleDialog() {
      this.addRoleVisible = true
    },
    closeRoleDialog() {
      this.addRoleVisible = false
    },
    editRole(id) {
      this.addRoleVisible = true
      // 直接获取子组件的实例对象 调用它内部的方法或者直接操作属性
      this.$refs.addRole.editRole(id)
    }

  }
}
</script>
